package com.quartz.image;


import com.alibaba.fastjson.JSONObject;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA.
 * User: lixiao
 * Created Date: 2020/9/10 16:14
 */
public class Mian {

    private BufferedImage getImageFromLocal(String path) throws IOException {
        return ImageIO.read(new File(path));

    }

    private BufferedImage mergeImage(BufferedImage img1, BufferedImage img2) {
            int width = img1.getWidth()/2;
            int height = img1.getHeight()/2;
            int width1 = img1.getWidth()/2;
            int height2 = img1.getHeight()/2;
            Graphics2D graphics2D = img2.createGraphics();
            graphics2D.drawImage(img1, 0, height2, width, height, null);
            graphics2D.dispose();
        return img2;
    }

    private void writeImageToLocal(String newPath, BufferedImage img) throws IOException {
            ImageIO.write(img, "png", new File(newPath));
    }

    public void saveMergeImage(String path1, String path2, String newPath) {
        System.out.println("合成图片开始");
        try {
            writeImageToLocal(newPath, mergeImage(getImageFromLocal(path1), getImageFromLocal(path2)));
            System.out.println("合成图片结束");
        } catch (IOException e) {
            System.out.println("合成图片异常");
            e.printStackTrace();
        }
    }

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.cm/file/6o58A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String body_seg() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg";
        try {
            // 本地文件路径
            String filePath = "G:\\timg.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.608bd4d02dd25e28b1fcbacda06c3cd5.2592000.1602319254.282335-22612789";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            JSONObject json = JSONObject.parseObject(result);
            String image= (String) json.get("foreground");
            System.out.println(image);
            Base64FileUitl.decoderBase64File(image,"G:\\mayun1.jpg");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=P46o3iRaVAE94Vzo1zGunKS8&client_secret=KDpcQAINOcEArXiGvrYGN02DSqrlVeqZ
    public static void main(String[] args) {
        Mian img = new Mian();
        String path1 = "G:\\mayun1.jpg";
        String path2 = "G:\\changcheng.jpg";
        String newPath = "G:\\gaoxiao.png";
        img.saveMergeImage(path1,path2,newPath);
        //body_seg();
    }
}